package com.gzhryc.shared_device.pcl.code.services;

import com.gzhryc.common.json.JsonTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.base.models.OrderLogContent;
import com.gzhryc.shared_device.pcl.code.dao.PCLOrderLogDao;
import com.gzhryc.shared_device.pcl.code.dao.db.PCLOrder;
import com.gzhryc.shared_device.pcl.code.dao.db.PCLOrderLog;

import java.sql.SQLException;
import java.util.Date;

public class PCLOrderLogService {

    static Logger log = Logger.getLogger(PCLOrderLogService.class);

    public static PCLOrderLogService self(Date date) {
        return MultiDBTools.getService(PCLOrderLogService.class, date);
    }

    public static PCLOrderLogService self(String jdbcKey, Date date) {
        return MultiDBTools.getService(jdbcKey, PCLOrderLogService.class, date);
    }

    PCLOrderLogDao dao;

    public PCLOrderLogService(String jdbcKey, Date date) {
        dao = new PCLOrderLogDao(jdbcKey,date);
    }

    public PCLOrderLog getByTradeNo(String tradeNo){
        try {
            return dao.getById(tradeNo);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public boolean add(PCLOrder deviceOrder, OperateInfo operateInfo, Integer state, String errorMsg) {
        OrderLogContent logContent = null;
        PCLOrderLog orderLog = getByTradeNo(deviceOrder.getTradeNo());
        boolean toAdd = false;
        if (orderLog == null) {
            orderLog = new PCLOrderLog();
            orderLog.setTradeNo(deviceOrder.getTradeNo());
            orderLog.setCreateDate(deviceOrder.getCreateDate());
            toAdd = true;
        } else if (JsonTools.isJsonStr(orderLog.getContent())) {
            logContent = JsonTools.fromJson(orderLog.getContent(), OrderLogContent.class);
        }

        if (logContent == null) {
            logContent = new OrderLogContent();
        }

        OrderLogContent.LogItem logItem = new OrderLogContent.LogItem();
        logItem.setCreateDate(new Date());
        logItem.setContent(operateInfo.getOperateNote());
        logItem.setProgramType(operateInfo.getProgramType());
        logItem.setProgramTypeName(operateInfo.getProgramTypeName());
        logItem.setOperatorType(operateInfo.getOperatorType());
        logItem.setOperatorTypeName(operateInfo.getOperatorTypeName());
        logItem.setOperator(operateInfo.getOperator());
        logItem.setState(state);
        logItem.setErrorMsg(errorMsg);
        logContent.getItems().add(logItem);

        orderLog.setContent(JsonTools.toJson(logContent));

        if (toAdd) {
            try {
                return dao.insert(orderLog) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        } else {
            try {
                return dao.updateNotNull(orderLog) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean add(PCLOrder deviceOrder, OrderLogContent logContent, OperateInfo operateInfo, Integer state, String errorMsg){
        PCLOrderLog orderLog = new PCLOrderLog();
        orderLog.setTradeNo(deviceOrder.getTradeNo());
        orderLog.setCreateDate(deviceOrder.getCreateDate());

        OrderLogContent.LogItem logItem = new OrderLogContent.LogItem();
        logItem.setCreateDate(new Date());
        logItem.setContent(operateInfo.getOperateNote());
        logItem.setProgramType(operateInfo.getProgramType());
        logItem.setProgramTypeName(operateInfo.getProgramTypeName());
        logItem.setOperatorType(operateInfo.getOperatorType());
        logItem.setOperatorTypeName(operateInfo.getOperatorTypeName());
        logItem.setOperator(operateInfo.getOperator());
        logItem.setState(state);
        logItem.setErrorMsg(errorMsg);
        logContent.getItems().add(logItem);

        orderLog.setContent(JsonTools.toJson(logContent));

        try {
            return dao.insert(orderLog) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }

        return false;
    }
}
